package com.tmc.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class CSVUtil {

	public static void readCSVFile1(String f)
	{
	    File csv = new File(f);  // CSV鏂囦欢璺緞
	    FileInputStream fis = null;
	    InputStreamReader isr = null;
	    BufferedReader br = null;
	    try
	    {
	    	fis = new FileInputStream(csv);
	    	isr = new InputStreamReader(fis,"UTF-8");
	    	br = new BufferedReader(isr);
	    } catch (FileNotFoundException e)
	    {
	        e.printStackTrace();
	    } catch (Exception e) {
			e.printStackTrace();
		}
	    String line = "";
	    String urlLine = "";
	    String hotelName = "";
	    String cityName = "";
	    String countryName = "";
	    try {
	            while ((line = br.readLine()) != null)  //璇诲彇鍒扮殑鍐呭缁檒ine鍙橀噺
	            {
	                urlLine = line;
	                boolean isNum = urlLine.contains("0")||urlLine.contains("1")||urlLine.contains("2")||urlLine.contains("3")||urlLine.contains("4")||urlLine.contains("5")||urlLine.contains("6")||urlLine.contains("7")||urlLine.contains("8")||urlLine.contains("9");
	                if(isNum){
	                	String[] strs = urlLine.split("[|]");
	                	hotelName = removeSpace(strs[1].substring(3,strs[1].length()-2));
	                	if(strs[12].length()==1){
	                		cityName = removeSpace(strs[13].substring(3,strs[13].length()-2));
	                	}else{
	                		cityName = removeSpace(strs[12].substring(1,strs[12].length()-1));
	                	}
	                	countryName = removeSpace(strs[17].substring(3,strs[17].length()-2));
	                	System.out.println(hotelName+"--"+cityName+"--"+countryName);
	                }
	            }
	    } catch (IOException e)
	    {
	        e.printStackTrace();
	    }
	}
	
	public static String removeSpace(String resource)   
    {   
        StringBuffer buffer=new StringBuffer();   
        int position=0;   
        int indexC = 0 ;
        int indexS = 0;
        String currentChar;   
        
        while(position<resource.length())   
        {   
            currentChar=resource.substring(position,position+1);
            position++;
            if(currentChar.matches("\\w|[:]|[/]|[.]|[&]|[-]")){
            	buffer.append(currentChar);
            	indexC ++;
            }else{
            	indexS ++;
            }
            if(indexS>indexC){
            	buffer.append(" ");
            	indexS--;
            }
            
        } 
        return buffer.toString().replace("  "," ");   
    }  
	
	public static void main(String[] args) {
		String path = "C:\\Users\\legion\\Desktop\\tourico闈欐�佹暟鎹甛\\HotelInfo\\PDS2_HotelInfo_THF.csv";
		readCSVFile1(path);
		
	}
	
}
